console.log('this is regist/index.js');

var _util = require('util/util.js');

var _account_service = require('service/account-service.js');

var userRegist={
    init :function(){
        $('#registMsg').hide();
        this.bindEvents();
        
        return this;
    },
    //绑定事件
   bindEvents : function(){
    var _this=this;
       $('#submit').click(
           function(){
            _this.submit();
           }
       )
   },
   submit : function(){
       var formData ={
        username : $.trim($('#username').val()),
        password : $.trim($('#password').val()),
        repassword : $.trim($('#repassword').val())
       };
       console.log(formData);
       var validationResult = this.formValidation(formData);
        console.log(validationResult);
        if(validationResult.status){
            //向服务器发送请求
            _account_service.regist(
                formData,
                function(res){
                    window.location.href ='./login.html';
                },
                function(errMsg){
                    $('#registMsg').show();
                }
            );
        }else{
            $('#registMsg').show().text(validationResult.msg);
        }
   },
   formValidation : function(formData){
    var result = {
        status :false,
        msg : ''
    };
    if( !_util.validateField('require',formData.username)){
        result.msg = '用户名不能为空';
        return result;
    }
    else if( !_util.validateField('require',formData.password)){
        result.msg = '密码不能为空';
        return result;
    }
    else if( !_util.validateField('require',formData.repassword)){
        result.msg = '未输入重复密码';
        return result;
    }
    else if(formData.password != formData.repassword){
        result.msg = '两次密码输入不一致';
        return result;
    }
    //通过
    result.status = true;
    result.msg='通过';
    return result;
   }
};

module.exports=userRegist.init();